package edu.ieslaferreria.creditsintesis;

import com.jme.math.Vector3f;
import com.jme.util.resource.RelativeResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import edu.ieslaferreria.creditsintesis.controllers.CollisionController;
import edu.ieslaferreria.creditsintesis.controllers.ExternalWorldController;
import edu.ieslaferreria.creditsintesis.controllers.InteractionController;
import edu.ieslaferreria.creditsintesis.controllers.SimpleCollisionController;
import edu.ieslaferreria.creditsintesis.controllers.UserActionController;
import edu.ieslaferreria.creditsintesis.logicgame.ObjectWithStates;
import edu.ieslaferreria.creditsintesis.logicgame.actions.BackAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.ForwardAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RotateLeftAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RotateRightAction;
import edu.ieslaferreria.creditsintesis.logicgame.actions.SimpleRunner;
import edu.ieslaferreria.creditsintesis.logicgame.actions.AddRemotePlayerEvent;
import edu.ieslaferreria.creditsintesis.rmi.Pont;
import edu.ieslaferreria.creditsintesis.rmi.PontImpl;
import edu.ieslaferreria.creditsintesis.rmi.PontServer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Clase Main
 * con esto iniciamos el jugador 2 que hará de cliente y cargará todos los
 * objetos y el mapa que ha creado el jugador1 (servidor).
 *  *  * 
 * @author Ruben
 */
public class MainPlayer2Rmi extends MainPlayerRmi{
//        private InteractionController interactionController;
        private CollisionController collisionController;
//        private ChaseCamera chaser;
//        private TerrainPage terrain;
//        private Sky sky;
        private ObjectWithStates fixedBox, fixedBox2;
        private PontServer server;
//        private String serverRmiUrl="localhost";
        private String serverRmiUrl="192.168.170.125";
        //        private HashMap<String,Node> remoteObjects = new HashMap<String, Node>();


	public static void main(String[] args) {
		// Signal to show properties dialog
		MainPlayer2Rmi app = new MainPlayer2Rmi(); // Create Object
		app.setConfigShowMode(ConfigShowMode.AlwaysShow);
		app.start(); // Start the program
	}

        @Override
    	protected void simpleInitGame() {
            display.setTitle("SocialSkills_P2");
            RelativeResourceLocator rrl = null;

            String pathForFlares= Main.class.getClassLoader().getResource("texture/flare1.png").getPath();
            pathForFlares = pathForFlares.substring(0, pathForFlares.lastIndexOf('/'));
            try {
                rrl = new RelativeResourceLocator(new URI("file://"+pathForFlares+"/"));
                Logger.getLogger(Main.class.getName()).log(Level.INFO, pathForFlares);
            } catch (URISyntaxException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, rrl);

//        System.setSecurityManager(new RMISecurityManager());

        try{
//            serverRmiUrl = System.getProperty("serverRmiUrl", "rmi://localhost/ServerImpl");
            serverRmiUrl = System.getProperty("serverRmiUrl", "rmi://192.168.170.125/ServerImpl");
            server = (PontServer)java.rmi.Naming.lookup(serverRmiUrl);
            createClient("Player2");

            //buildWorld();
            buildControllers();
//            buildEnvironment();

            Pont p=server.addClient(0, new PontImpl(client.getListener(), client.getNom()), "Player2",true);
            client.setPont(p);
            interactionController = new InteractionController(actionStates,client.getPont());

            buildPlayer("Player2",new Vector3f(95,0, 150),0.02f,"model/zack.obj","ZackModel",0f,2f);
            //System.out.println(p.getPontName());
            p.send(new AddRemotePlayerEvent(player.getConfigurationEvent()));


            //buildFixedObjects();
            buildChaseCamera();

            

        }catch(NotBoundException nbe){
            nbe.printStackTrace();
        }catch(RemoteException re){
            re.printStackTrace();
        }catch(MalformedURLException mue){
            mue.printStackTrace();
        }
        }

     /**
     * create our custom input handler.
     *
     */
    private void buildControllers() throws RemoteException {
        userActionController = new UserActionController(player, settings.getRenderer(), this.actionStates, input);
        collisionController = new SimpleCollisionController(this.actionStates);
        worldController = new ExternalWorldController(terrain, actionStates, collisionController);
        //worldController = new CaveWorldController(terrain, actionStates, collisionController);
        worldController.setAction(SimpleRunner.AC_GO_FORWARD, new ForwardAction());
        worldController.setAction(SimpleRunner.AC_SPIN_LEFT, new RotateLeftAction());
        worldController.setAction(SimpleRunner.AC_SPIN_RIGHT, new RotateRightAction());
        worldController.setAction(SimpleRunner.AC_GO_BACK, new BackAction());

        //interactionController = new InteractionController(actionStates,client.getPont());

        }

}
